# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField


class AdminMenuDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertAdminMenu(self, info):
        """
        插入admin_menu信息
        :param info:
        :return:
        """
        menuParentId = info['menuParentId']
        menuType = info['menuType']
        menuTitle = info['menuTitle']
        menuIcoUrl = info['menuIcoUrl']
        menuLinkUrl = info['menuLinkUrl']
        menuSort = info['menuSort']
        isLock = info['isLock']
        remark = info['remark']
        actionKey = info['actionKey']
        isSys = info['isSys']

        sql = "insert into zzh_admin_menu (menu_parent_id,menu_type,menu_title,menu_ico_url,menu_link_url," \
              "menu_sort,is_lock,remark,action_key,is_sys) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [menuParentId, menuType, menuTitle, menuIcoUrl, menuLinkUrl, menuSort, isLock,
                               remark, actionKey, isSys])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findMenuById(self, menuId):
        """
        根据ID查询admin_menu信息
        :param menuId:
        :return:
        """
        sql = "select * from zzh_admin_menu WHERE menu_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [menuId])
        menuJson = None
        if rs:
            list0 = cur.fetchall()
            menuJson = Table2Json.admin_menu_list_to_json(list0)
        cur.close()
        self.conn.close()
        return menuJson

    def findMenuByCondition(self, **kwargs):
        """
        admin_menu表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        fieldMap = requestFieldMapToDBField.adminMenuFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_admin_menu WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = sql = "SELECT * FROM zzh_admin_menu WHERE " + whereSentence
        rs = cur.execute(sql)
        menuJson = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, rsSet)
            menuJson = Table2Json.admin_menu_list_to_json(list0)
        cur.close()
        self.conn.close()
        return menuJson, rs

    def deleteAdminMenu(self, idArray):
        """
        根据ID删除信息，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_admin_menu WHERE menu_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateAdminMenu(self, info):
        """
        更新admin_menu信息
        :param info:
        :return:
        """
        menuId = info['menuId']
        menuParentId = info['menuParentId']
        menuType = info['menuType']
        menuTitle = info['menuTitle']
        menuIcoUrl = info['menuIcoUrl']
        menuLinkUrl = info['menuLinkUrl']
        menuSort = info['menuSort']
        isLock = info['isLock']
        remark = info['remark']
        actionKey = info['actionKey']
        isSys = info['isSys']

        sql = "update zzh_admin_menu set menu_parent_id=%s,menu_type=%s,menu_title=%s,menu_ico_url=%s,menu_link_url=%s," \
              "menu_sort=%s,is_lock=%s,remark=%s,action_key=%s,is_sys=%s " \
              " WHERE menu_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [menuParentId, menuType, menuTitle, menuIcoUrl, menuLinkUrl, menuSort, isLock, remark,
                               actionKey, isSys, menuId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectAdminMenu(self, menuId):
        """
        根据ID查询数据，返回元组
        :param menuId:
        :return: data tuple
        """
        sql = "select * from zzh_admin_menu WHERE menu_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [menuId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def find_by_actionkey(self, **kwargs):
        """
        查找不在actionKey中的菜单信息
        :param kwargs:
        :return:
        """
        actionKey = kwargs['actionKey']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        length = len(actionKey)
        if length == 1:
            sql = "select * from zzh_admin_menu WHERE action_key!=%s" % actionKey[0]
        elif length == 0:
            sql = "select * from zzh_admin_menu"
        else:
            # a = {"aa": tuple(actionKey)}
            keys = tuple([actionKey[index].encode("utf-8") for index in range(length)])
            sql = "select * from zzh_admin_menu WHERE action_key NOT IN %s" % (keys,)

        cur = self.conn.cursor()
        rs = cur.execute(sql)
        menuJson = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, rsSet)
            menuJson = Table2Json.admin_menu_list_to_json(list0)
        cur.close()
        self.conn.close()
        return menuJson, rs